//
//  JWCalendarListEditTableViewController.m
//  SiqiCalendar
//
//  Created by sunjiawen on 2018/4/9.
//  Copyright © 2018年 sjw. All rights reserved.
//

#import "JWCalendarListEditTableViewController.h"
#import "JWCalendarListViewModel.h"
#import "UIScrollView+EmptyDataSet.h"

static NSString * const kCalendarListCellID = @"EditCalendarListCellID";

@interface JWCalendarListEditTableViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic, strong)JWCalendarListViewModel *viewModel;
@property(nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation JWCalendarListEditTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
}

- (void)setupUI {
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"cancel"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
}

- (void)loadData {
    [self.viewModel loadDataWithBlock:^(NSArray *list, CommonError *error) {
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:list];
        
        [self.tableView reloadData];
    }];
}

#pragma mark - Actions
- (void)leftBarButtonItemAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - DZNEmptyDataSetSource Methods
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"nodata-2"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = kNoDataText;
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: JWConfigShareInstance.noDataTextFont,
                                 NSForegroundColorAttributeName: JWConfigShareInstance.noDataTextColor,
                                 NSParagraphStyleAttributeName: paragraphStyle};
    
    return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}


#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return NO;
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCalendarListCellID forIndexPath:indexPath];

    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}



// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}



// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:!self.isEditing];
    
    if (!self.isEditing) {
        
    }else {
        
    }
}


#pragma mark - Getter
- (JWCalendarListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[JWCalendarListViewModel alloc]init];
    }
    return _viewModel;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
